package com.westa.smallshop.shop;

import android.support.annotation.NonNull;

import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.data.source.GoodsItemDataSource;
import com.westa.smallshop.data.source.remote.BaseDataSource;

import java.util.ArrayList;
import java.util.List;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by cy on 10/26/16.
 */

public class ShopPresenter implements ShopContract.Presenter, BaseDataSource.LoadListCallBack<GoodsItem> {

    @NonNull
    private final ShopContract.View<GoodsItem> mView;
    @NonNull
    private final GoodsItemDataSource mGoodsItemDataSource;

    private List<String> mPagerImageUrl;

    public ShopPresenter(@NonNull ShopContract.View<GoodsItem> view,
                         @NonNull GoodsItemDataSource goodsItemDataSource) {
        mGoodsItemDataSource = checkNotNull(goodsItemDataSource);
        mView = checkNotNull(view);

        mView.setPresenter(this);
    }

    @Override
    public void refreshData() {
        mGoodsItemDataSource.getGoods(BaseDataSource.REFRESH, "1", this);
    }

    @Override
    public void loadMoreData(String currentPage) {
        mGoodsItemDataSource.getGoods(BaseDataSource.LOAD_MORE, currentPage, this);
    }

    @Override
    public List<String> getPagerImageUrl() {
        //TODO
        mPagerImageUrl = new ArrayList<>();
//        mPagerImageUrl.add("http://pic3.bbzhi.com/fengjingbizhi/gaoqingkuanpingfengguangsheyingps/show_fengjingta_281299_11.jpg");
//        mPagerImageUrl.add("http://www.bz55.com/uploads1/allimg/120312/1_120312100435_8.jpg");
//        mPagerImageUrl.add("http://img1.3lian.com/2015/w2/10/d/63.jpg");
        mPagerImageUrl.add("http://img.hb.aicdn.com/8544ce5e9fa3de9cc0e4f1f80c28a2d8da2f926152cbc-pGu5R0_fw658");
        mPagerImageUrl.add("http://img.hb.aicdn.com/e56ee55289489c8a7bbd988400d30c605754d87f780a3-GVY5u0_fw658");
        mPagerImageUrl.add("http://img.hb.aicdn.com/dc1c1ae0a519f3fa3b85fce46fef278ade1c3705153363-z1zX7b_fw658");
        return mPagerImageUrl;
    }

    @Override
    public void start() {
        refreshData();
    }

    @Override
    public void destroy() {
        mGoodsItemDataSource.cancelLoadRequest();
    }

    @Override
    public void onRefreshDataSuccessful(List<GoodsItem> data) {
        if (mView.isActive()) {
            mView.setRefreshing(false);
            mView.showRefreshSuccessful(data);
            mView.setEmptyViewVisibility(false);
        }
    }

    @Override
    public void onLoadMoreDataSuccessful(List<GoodsItem> data) {
        if (mView.isActive()) {
            mView.setRefreshing(false);
            mView.loadMoreData(data);
        }
    }

    @Override
    public void onLoadDataFailed() {
        mView.setRefreshing(false);
    }
}
